package com.xjnt.base.support.admin.controller;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xjnt.base.support.admin.entity.Account;
import com.xjnt.base.support.admin.entity.Group;
import com.xjnt.base.support.admin.entity.Role;
import com.xjnt.base.support.admin.service.GroupService;
import com.xjnt.base.support.admin.service.RoleService;
import com.xjnt.frameworks.annotation.AutoInject;
import com.xjnt.frameworks.annotation.Router;
import com.xjnt.frameworks.core.CRUDController;
import com.xjnt.frameworks.exception.BusinessException;
import com.xjnt.frameworks.web.message.RetMsg;

@Router(name="/system/admin/group", view="/admin/group")
public class GroupController extends CRUDController<Group, GroupService> {

	@AutoInject
	private GroupService groupService;
	
	@AutoInject
	private RoleService roleService;
	
	@Override
	protected String getBizName() {
		return "用户组";
	}
	
	@Override
	protected Class<Group> getClazz() {
		return Group.class;
	}

	@Override
	protected GroupService getService() {
		return groupService;
	}

	/**
	 * 分配角色
	 */
	public void allocRole(){
		//参数保持传递
		keepPara();
		// 登陆状态才能进行分配，则先获取登陆用户所拥有的角色。
		Account account = (Account) getCurrUser();
		List<Role> userRoles = Lists.newArrayList();
		if(account.getInt(Account.SUPER) == 1){ //超级管理员
			userRoles = roleService.findAll();
		} else {
			//查询直接角色
			userRoles = roleService.findByAccountId(account.getStr(Account.PK_ID));
			//查询用户隶属组
			List<Group> groups = groupService.findByAccountId(account.getStr(Account.PK_ID));
			if(null != groups && !groups.isEmpty()){
				List<Role> groupRoles = roleService.findByGroupIds(toIds(groups));
				userRoles.removeAll(groupRoles);
				userRoles.addAll(groupRoles);
			}
		}
		// 页面传递的目标分配组ID
		String groupId = getPara("groupId");
		List<Role> targetGroupRoles = roleService.findByGroupId(groupId);
		
		// 求并集
		userRoles.removeAll(targetGroupRoles);
		userRoles.addAll(targetGroupRoles);
		// 对角色进行过滤,定义前端数据
		List<Map<String, String>> finalRoles = Lists.newArrayList();
		for (Role role : userRoles) {
			Map<String, String> finalRole = Maps.newHashMap();
			finalRole.put("name", role.getStr(Role.NAME));
			finalRole.put("val", role.getStr(Role.PK_ID));
			if(targetGroupRoles.contains(role)){
				finalRole.put("selected", "selected");
			}
			finalRoles.add(finalRole);
		}
		setAttr("allocRole", finalRoles);
		render("allocRole.html");
	}
	
	public void doAllocRole(){
		RetMsg retMsg = new RetMsg();
		try{
			// 获得页面分配的角色标识集合
			String[] roleIds = getPara("allocRoleIds") == null ? null : getPara("allocRoleIds").split(",");
			if(null != roleIds){ //角色标识集合不为空
				List<Role> newGroupRoles = roleService.findByIds(roleIds);
				// 页面传递的目标分配组ID
				String groupId = getPara("groupId");
				List<Role> originalGroupRoles = roleService.findByGroupId(groupId);
				// 定义原角色与新角色的交集
				List<Role> retainRoles = Lists.newArrayList();
				retainRoles.addAll(originalGroupRoles);
				retainRoles.retainAll(newGroupRoles);
				// 从新的角色中去掉原来已增加过的角色
				newGroupRoles.removeAll(retainRoles);
				// 原角色中去掉交集角色，就是需要删除的角色
				originalGroupRoles.removeAll(retainRoles);
				if(!originalGroupRoles.isEmpty()){
					getService().removeRoles(groupId, originalGroupRoles);
				}
				// 保存新分配的用户组角色关系
				if(!newGroupRoles.isEmpty()){ //如果不为空，则保存
					getService().allocRoles(groupId, newGroupRoles);
				}
			} else { //角色集合为null时，删除该组所有角色关系
				// 页面传递的目标分配组ID
				String groupId = getPara("groupId");
				getService().removeAllRole(groupId);
			}
			retMsg.pushOk("分配角色成功");
			renderJson(retMsg);
		} catch (Exception ex){
			throw new BusinessException("用户组分配角色", "发生异常：", ex);
		}
	}
}
